package com.bidepan.spring6.resource;

import org.springframework.core.io.ClassPathResource;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

//访问类路径下的资源
public class ClassPathResourceDemo {
    public static void loadClassPathResources(String path){
        //创建对象ClassPathResource
        ClassPathResource resource = new ClassPathResource(path);

        System.out.println(resource.getFilename());
        System.out.println(resource.getDescription());
        //获取文件内容
        try {
            InputStream in = resource.getInputStream();
            int available = in.available();
            System.out.println("Available bytes: " + available);
            byte[] bytes = new byte[available];
            while(in.read(bytes) != -1){
                System.out.println(new String(bytes));
            }
            in.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {
        loadClassPathResources("demo.txt");
    }
}
